package com.hex.campus.backend.hex_campus_backend.service;

import com.hex.campus.backend.hex_campus_backend.entity.Partner;
import com.hex.campus.backend.hex_campus_backend.entity.PartnerMatch;
import com.hex.campus.backend.hex_campus_backend.entity.User;
import com.hex.campus.backend.hex_campus_backend.repository.PartnerMatchRepository;
import com.hex.campus.backend.hex_campus_backend.repository.PartnerRepository;
import com.hex.campus.backend.hex_campus_backend.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; // 使用正确的Pageable类型
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class PartnerService {
    private final PartnerRepository partnerRepository;
    private final PartnerMatchRepository matchRepository;
    private final UserRepository userRepository;

    // 创建伙伴请求
    public Partner createPartner(Partner partner, Long userId) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new EntityNotFoundException("User not found with ID: " + userId));
        partner.setUser(user);
        return partnerRepository.save(partner);
    }

    // 获取当前用户的所有活动伙伴请求
    public Page<Partner> getMyPartners(Long userId, Pageable pageable) {
        return partnerRepository.findByUserIdAndStatus(userId, Partner.PartnerStatus.ACTIVE, pageable);
    }

    public Partner getPartnerById(Long id) {
        return partnerRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("找不到ID为 " + id + " 的帖子"));
    }

    // 删除伙伴请求
    @Transactional
    public void deletePartner(Long partnerId, Long userId) {
        Partner partner = partnerRepository.findById(partnerId)
                .orElseThrow(() -> new EntityNotFoundException("Partner not found with ID: " + partnerId));

        if (!partner.getUser().getId().equals(userId)) {
            throw new IllegalArgumentException("Not authorized to delete this partner request");
        }

        partnerRepository.delete(partner); // 可以考虑软删除
    }

    // 根据主题筛选并获取活动伙伴请求
    public Page<Partner> getPartners(Pageable pageable, String subjectFilter) {
        if (subjectFilter != null) {
            return partnerRepository.findBySubjectContainingAndStatus(subjectFilter, Partner.PartnerStatus.ACTIVE, pageable);
        }
        return partnerRepository.findByStatus(Partner.PartnerStatus.ACTIVE, pageable);
    }

    // 创建匹配请求
    @Transactional
    public PartnerMatch createMatch(Long partnerId, Long userId) {
        if (matchRepository.existsByPartnerIdAndMatcherId(partnerId, userId)) {
            throw new IllegalArgumentException("Already matched this partner request");
        }

        Partner partner = partnerRepository.findById(partnerId)
                .orElseThrow(() -> new EntityNotFoundException("Partner not found with ID: " + partnerId));

        User user = userRepository.findById(userId)
                .orElseThrow(() -> new EntityNotFoundException("User not found with ID: " + userId));

        if (partner.getUser().getId().equals(userId)) {
            throw new IllegalArgumentException("Cannot match your own request");
        }

        PartnerMatch match = new PartnerMatch();
        match.setPartner(partner); // 确保此处方法存在
        match.setMatcher(user);
        match.setMatchedAt(LocalDateTime.now());

        return matchRepository.save(match);
    }
}

